export function spliceImgToBase64(imgUrl: string): Promise<string[]> {
  return new Promise((res) => {
    // 创建一个新的Image对象
    const img = new Image();
    const list: string[] = [];
    img.crossOrigin = 'Anonymous';
    // 设置图片源并启动加载
    img.src = imgUrl;
    img.onload = function () {
      // 假设图片是一个正方形，如果不是，你可能需要调整这些值
      const imgWidth = img.width;
      const imgHeight = img.height;
      const quarterWidth = imgWidth / 2;
      const quarterHeight = imgHeight / 2;

      // 裁剪并显示四个部分
      for (let i = 0; i < 2; i++) {
        for (let j = 0; j < 2; j++) {
          const quarterCanvas = document.createElement('canvas');
          quarterCanvas.width = quarterWidth;
          quarterCanvas.height = quarterHeight;
          const quarterCtx = quarterCanvas.getContext('2d');
          // 裁剪区域
          quarterCtx?.drawImage(img, j * quarterWidth, i * quarterHeight, quarterWidth, quarterHeight, 0, 0, quarterWidth, quarterHeight);
          list.push(base64ToBlobURL(quarterCanvas.toDataURL().toString(), 'image/png'));
        }
      }

      res(list);
    };
  });
}

function base64ToBlobURL(base64Str: string, contentType: 'image/png') {
  const parts = base64Str.split(';base64,');
  const raw = window.atob(parts[1]);
  const rawLength = raw.length;
  const uInt8Array = new Uint8Array(rawLength);

  for (let i = 0; i < rawLength; ++i) {
    uInt8Array[i] = raw.charCodeAt(i);
  }

  const blob = new Blob([uInt8Array], { type: contentType });
  return URL.createObjectURL(blob);
}
